<?php

class DeliveryAction extends CommonAction{
public function index()
{
parent::index();
}
public function add()
{
$weight_list = M('WeightUnit')->findAll();
$this->assign('weight_list',$weight_list);
$this->assign('new_sort',M('Delivery')->max('sort')+1);
$this->display();
}
public function insert() {
B('FilterString');
$ajax = intval($_REQUEST['ajax']);
$data = M(MODULE_NAME)->create ();
$this->assign('jumpUrl',u(MODULE_NAME.'/add'));
if(!check_empty($data['name']))
{
$this->error(L('DELIVERY_NAME_EMPTY_TIP'));
}
$log_info = $data['name'];
$list=M(MODULE_NAME)->add($data);
require_once APP_ROOT_PATH.'system/utils/child.php';
$child = new child('delivery_region');
if (false !== $list) {
$delivery_regions = $_REQUEST['region_support_region'];
foreach($delivery_regions as $k=>$v)
{
if($v!='')
{
$id_arr = explode(',',$v);
$sub_ids = array();
foreach($id_arr as $vv)
{
if(!in_array($vv,$sub_ids))
{
$tmp_ids = $child->getChildIds($vv);
$tmp_ids[] = $vv;
$sub_ids = array_merge($sub_ids,$tmp_ids);
}
}
$delivery_fee_item = array();
$delivery_fee_item['delivery_id'] = $list;
$delivery_fee_item['region_ids'] = implode(',',$sub_ids);
$delivery_fee_item['first_weight'] = $_REQUEST['region_first_weight'][$k];
$delivery_fee_item['first_fee'] = $_REQUEST['region_first_fee'][$k];
$delivery_fee_item['continue_weight'] = $_REQUEST['region_continue_weight'][$k];
$delivery_fee_item['continue_fee'] = $_REQUEST['region_continue_fee'][$k];
M('DeliveryFee')->add($delivery_fee_item);
}
}
save_log($log_info.L('INSERT_SUCCESS'),1);
clear_auto_cache('cache_support_delivery');
$this->success(L('INSERT_SUCCESS'));
}else {
save_log($log_info.L('INSERT_FAILED'),0);
$this->error(L('INSERT_FAILED'));
}
}
public function edit() {
$id = intval($_REQUEST ['id']);
$condition['id'] = $id;
$vo = M(MODULE_NAME)->where($condition)->find();
$this->assign ( 'vo',$vo );
$weight_list = M('WeightUnit')->findAll();
$this->assign('weight_list',$weight_list);
$regions_list = M('DeliveryFee')->where('delivery_id='.$id)->findAll();
foreach($regions_list as $k=>$v)
{
$names = '';
$regions = M('DeliveryRegion')->where('id in('.$v['region_ids'].')')->findAll();
foreach($regions as $kk=>$vv)
{
$names.=$vv['name'].',';
}
$names = substr($names,0,-1);
$regions_list[$k]['names'] = $names;
}
$this->assign('regions_list',$regions_list);
$this->display ();
}
public function update() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$log_info = M(MODULE_NAME)->where('id='.intval($data['id']))->getField('name');
$this->assign('jumpUrl',u(MODULE_NAME.'/edit',array('id'=>$data['id'])));
if(!check_empty($data['name']))
{
$this->error(L('DELIVERY_NAME_EMPTY_TIP'));
}
require_once APP_ROOT_PATH.'system/utils/child.php';
$child = new child('delivery_region');
$list=M(MODULE_NAME)->save ($data);
if (false !== $list) {
M('DeliveryFee')->where('delivery_id='.$data['id'])->delete();
$delivery_regions = $_REQUEST['region_support_region'];
foreach($delivery_regions as $k=>$v)
{
if($v!='')
{
$id_arr = explode(',',$v);
$sub_ids = array();
foreach($id_arr as $vv)
{
if(!in_array($vv,$sub_ids))
{
$tmp_ids = $child->getChildIds($vv);
$tmp_ids[] = $vv;
$sub_ids = array_merge($sub_ids,$tmp_ids);
}
}
$delivery_fee_item = array();
$delivery_fee_item['delivery_id'] = $data['id'];
$delivery_fee_item['region_ids'] = implode(',',$sub_ids);
$delivery_fee_item['first_weight'] = $_REQUEST['region_first_weight'][$k];
$delivery_fee_item['first_fee'] = $_REQUEST['region_first_fee'][$k];
$delivery_fee_item['continue_weight'] = $_REQUEST['region_continue_weight'][$k];
$delivery_fee_item['continue_fee'] = $_REQUEST['region_continue_fee'][$k];
M('DeliveryFee')->add($delivery_fee_item);
}
}
save_log($log_info.L('UPDATE_SUCCESS'),1);
clear_auto_cache('cache_support_delivery');
$this->success(L('UPDATE_SUCCESS'));
}else {
save_log($log_info.L('UPDATE_FAILED'),0);
$this->error(L('UPDATE_FAILED'),0,$log_info.L('UPDATE_FAILED'));
}
}
public function foreverdelete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->delete();
if ($list!==false) {
M('DeliveryFee')->where(array ('delivery_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('FreeDelivery')->where(array ('delivery_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
save_log($info.l('FOREVER_DELETE_SUCCESS'),1);
clear_auto_cache('cache_support_delivery');
$this->success (l('FOREVER_DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('FOREVER_DELETE_FAILED'),0);
$this->error (l('FOREVER_DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function set_sort()
{
$id = intval($_REQUEST['id']);
$sort = intval($_REQUEST['sort']);
$log_info = M(MODULE_NAME)->where('id='.$id)->getField('name');
if(!check_sort($sort))
{
$this->error(l('SORT_FAILED'),1);
}
M(MODULE_NAME)->where('id='.$id)->setField('sort',$sort);
clear_auto_cache('cache_support_delivery');
save_log($log_info.l('SORT_SUCCESS'),1);
$this->success(l('SORT_SUCCESS'),1);
}
public function set_effect()
{
$id = intval($_REQUEST['id']);
$ajax = intval($_REQUEST['ajax']);
$info = M(MODULE_NAME)->where('id='.$id)->getField('name');
$c_is_effect = M(MODULE_NAME)->where('id='.$id)->getField('is_effect');
$n_is_effect = $c_is_effect == 0 ?1 : 0;
M(MODULE_NAME)->where('id='.$id)->setField('is_effect',$n_is_effect);
save_log($info.l('SET_EFFECT_'.$n_is_effect),1);
clear_auto_cache('cache_support_delivery');
$this->ajaxReturn($n_is_effect,l('SET_EFFECT_'.$n_is_effect),1)	;
}
public function selectRegions()
{
$delivery_regions = M('DeliveryRegion')->where('region_level = 1')->findAll();
$region_conf_id = intval($_REQUEST['region_conf_id']);
$delivery_fee = M('DeliveryFee')->where('id='.$region_conf_id)->find();
$delivery_fee['region_ids'] = explode(',',$delivery_fee['region_ids']);
foreach($delivery_regions as $k=>$v)
{
$delivery_regions[$k]['delivery_regions'] = M('DeliveryRegion')->where('pid = '.$v['id'])->findAll();
}
$this->assign('delivery_regions',$delivery_regions);
$this->assign('delivery_fee',$delivery_fee);
$this->display();
}
public function getSubRegion()
{
$id = intval($_REQUEST['id']);
$region_conf_id = intval($_REQUEST['delivery_fee_id']);
$delivery_fee = M('DeliveryFee')->where('id='.$region_conf_id)->find();
$delivery_fee['region_ids'] = explode(',',$delivery_fee['region_ids']);
$delivery_regions = M('DeliveryRegion')->where('pid = '.$id)->findAll();
$this->assign('delivery_regions',$delivery_regions);
$this->assign('delivery_fee',$delivery_fee);
$this->display();
}
}

?>